#!/usr/bin/env python
#-*- coding:utf-8 -*-
import os,sys,glob
from jpype import *

class IMC(object):
    def __init__(self,jar_path='/work/opt/analytic/my_python/java_lib/imc',
                 jvm_path='/opt/soft/java/jre/lib/amd64/server/libjvm.so',
                 scf_cfg='/work/opt/analytic/my_python/config/scf.config',):
        self.jars=':'.join(glob.glob('%s/*.jar' % jar_path))
        self.jvm_path=jvm_path
        self.scf_cfg=scf_cfg
        
    def start(self):
        startJVM(self.jvm_path,"-ea","-Djava.class.path=%s" % self.jars)
        SCFInit=JClass("com.bj58.spat.scf.client.SCFInit")
        SCFInit.init(self.scf_cfg)
        URL="tcp://imc/InfoProviderWithIndex"
        ProxyFactory=JClass("com.bj58.spat.scf.client.proxy.builder.ProxyFactory")
        Info=JClass("com.bj58.sfft.imc.contract.IInfoService")
        self.Integer=JClass("java.lang.Integer")
        self.String=JClass("java.lang.String")
        self.proxy=ProxyFactory.create(Info,URL)
        self.out=JClass("com.bj58.spat.scf.server.contract.entity.Out")
        
    def get_info(self,info_id,attrs="params,cateid,url,state,locals,userip"):
        info_id=long(info_id)
        return self.proxy.GetInfo(info_id,attrs)
    
    def get_info_list(self,condition,page_size,page_num):
        total=self.out()
        total.setOutPara(0)
        return self.proxy.GetInfo(condition,"infoid,locals",page_size,page_num,total)
    
    def update_info(self,info_id,ts):
        info_id=long(info_id)
        self.proxy.SetInfoTop(info_id,ts)
        
    def stop(self):
        shutdownJVM()
        
